﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcademicPlanner.Framework.Libraries.Auth
{
	/// <summary>
	/// DBorOpenIdBAuthenticator implements authentication
	/// using either an OpenId provider or database as the
	/// authentication list storage. A DBorOpenIdBAuthenticator
	/// uses a <see cref="T:DBBAuthenticator"/> and a
	/// <see cref="T:OpenIdBAuthenticator"/>.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public class DBorOpenIdBAuthenticator : IAuthenticator
	{
		private DBBAuthenticator dbAuthN;

		private OpenIdBAuthenticator opdAuthN;


		/// <summary>
		/// Constructs a DBorOpenIdBAuthenticator with the given
		/// connection string for db based authentication.
		/// </summary>
		/// <param name="pConnection">The connection string to the db.</param>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public DBorOpenIdBAuthenticator(string pConnection)
		{
			dbAuthN = new DBBAuthenticator(pConnection);
			opdAuthN = new OpenIdBAuthenticator();
		}


		/// <summary>
		/// Authenticates a user.
		/// </summary>
		/// <param name="pUser">The user's identifier.</param>
		/// <param name="pPassword">The user's password</param>
		/// <returns>
		/// An <c>AuthenticationResult</c> detailing the result of the authentication request.
		/// </returns>
		/// <seealso cref="M:DBBAuthenticator.Authenticate"/>
		/// <seealso cref="M:OpenIdBAuthenticator.Authenticate"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public AuthenticationResult Authenticate(string pUser, string pPassword)
		{
			// TODO: DECIDE BASED ON USER IDENTIFIER FORMAT (AND MAY BE PPASSWORD == NULL) WHETHER TO USE OPENID OR DB
			return dbAuthN.Authenticate(pUser, pPassword);
		}

		/// <summary>
		/// Authenticates a user. This method is a convenience overload
		/// of <see cref="M:Authenticate(string, string)"/> with the
		/// second argument <c>null</c>.
		/// </summary>
		/// <param name="pUser">The user's identifier.</param>
		/// <returns>
		/// An <c>AuthenticationResult</c> detailing the result of the authentication request.
		/// </returns>
		/// <seealso cref="M:Authenticate(string, string)"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public AuthenticationResult Authenticate(string pUser)
		{
			return Authenticate(pUser, null);
		}
	}
}
